<template>
    <div>
        <el-card class="box-card">
            <div slot="header" class="clearfix box-header">
                <span>电商管理系统</span>
            </div>
            <el-form
                :model="ruleForm"
                status-icon
                :rules="rules"
                ref="ruleForm"
                label-width="100px"
                class="demo-ruleForm"
            >
                <el-form-item label="用户名：" prop="username">
                    <el-input v-model="ruleForm.username"></el-input>
                </el-form-item>
                <el-form-item label="密码：" prop="password">
                    <el-input type="password" v-model="ruleForm.password"></el-input>
                </el-form-item>

                <el-form-item>
                    <el-button type="primary" @click="submitForm('ruleForm')">提交</el-button>
                    <el-button @click="resetForm('ruleForm')">重置</el-button>
                </el-form-item>
            </el-form>
        </el-card>
    </div>
</template>

<script>
export default {
    name: "login",
    data: function() {
        /*
    rule 验证规则
    value 将要进行验证的字符串
    callback 验证成功或失败的回调函数
    */
        const validateUsername = (rule, value, callback) => {
            console.log(rule, value);
            //将value 去掉 左右空格
            const myvalue = value.trim();
            const reg = /^[a-zA-Z0-9]{4,16}$/;
            if (!reg.test(myvalue)) {
                //验证不通过
                callback(new Error("请输入4-16位的数字或大小写字母"));
            } else {
                //验证通过
                callback();
            }
        };
        return {
            ruleForm: {
                username: "",
                password: "",
            },
            rules: {
                //正则验证
                username: [
                    { required: true, trigger: "blur", message: "请输入用户名" },
                    { validator: validateUsername, trigger: "blur" }, //validator:validateUsername 自定义验证
                ],
                password: [
                    { required: true, trigger: "blur", message: "请输入密码" },
                    { min: 4, max: 8, message: "密码请输入4-8位" },
                ],
            },
        };
    },
    methods: {
        //表单提交
        submitForm(formName) {
            console.log(formName);
            this.$refs[formName].validate((valid) => {
                if (valid) {
                    //验证通过
                    console.log("可以调用登录接口");
                    //希望调用 vuex的actions 来完成登录效果
                    this.$store.dispatch("LoginAC", this.ruleForm).then((res) => {
                        console.log(83, res);
                        this.$message({
                            message: "登录成功",
                            type: "success",
                        });
                        if (res.status == 200) {
                            this.$router.push("/");
                        }
                    });
                } else {
                    console.log("验证没通过");
                }
            });
        },
    },
};
</script>

<style lang="scss" scoped>
.box-card {
    width: 500px;
    margin: 200px auto;
    .box-header {
        background: skyblue;
        font-size: 30px;
        font-weight: bold;
        height: 100px;
        line-height: 100px;
        text-align: center;
        color: #fff;
    }
}
</style>
